package com.ramotion.expandingcollection;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Image;
import ohos.agp.components.PageFlipper;
import ohos.app.Context;

import java.util.List;

/**
 * FeatureAbility
 *
 * @since 2021-05-31
 */
public class EcBackgroundSwitcherView extends PageFlipper {
    private static final int ALPHA_DURATION = 400;
    private static final int MOVEMENT_DURATION = 500;
    private static final int GAP_PERCENT = 12;
    private static final int CONSTANT_2 = 2;
    private static final int CONSTANT_100 = 100;
    private AnimatorProperty bgImageInLeftAnimation;
    private AnimatorProperty bgImageOutLeftAnimation;

    private AnimatorProperty bgImageInRightAnimation;
    private AnimatorProperty bgImageOutRightAnimation;

    private AnimationDirection currentAnimationDirection;

    private List<EcCardData> dataSet;

    /**
     * EcBackgroundSwitcherView
     *
     * @param context
     */
    public EcBackgroundSwitcherView(Context context) {
        this(context, null);
        inflateAndInit(context);
    }

    /**
     * EcBackgroundSwitcherView
     *
     * @param context
     * @param attrs
     */
    public EcBackgroundSwitcherView(Context context, AttrSet attrs) {
        super(context, attrs);
        inflateAndInit(context);
    }

    /**
     * inflateAndInit
     *
     * @param context
     */
    private void inflateAndInit(final Context context) {
        int bgImageGap = (ResUtils.getScreenWidth(context) / CONSTANT_100) * GAP_PERCENT;
        int bgImageWidth = ResUtils.getScreenWidth(context) + bgImageGap * CONSTANT_2;

        Image myView = new Image(context);
        myView.setScaleMode(Image.ScaleMode.CLIP_CENTER);
        myView.setLayoutConfig(new LayoutConfig(bgImageWidth, LayoutConfig.MATCH_PARENT));
        myView.setTranslationX(-bgImageGap);
        addComponent(myView);

        bgImageInLeftAnimation = createBgImageInAnimation(0, -bgImageGap, MOVEMENT_DURATION, ALPHA_DURATION);
        bgImageOutLeftAnimation = createBgImageOutAnimation(0, -bgImageGap, MOVEMENT_DURATION);
        bgImageInRightAnimation = createBgImageInAnimation(-bgImageGap, 0, MOVEMENT_DURATION, ALPHA_DURATION);
        bgImageOutRightAnimation = createBgImageOutAnimation(0, bgImageGap, MOVEMENT_DURATION);
    }

    /**
     * setImageBitmapWithAnimation
     *
     * @param newBitmap
     * @param animationDirection
     */
    private void setImageBitmapWithAnimation(int newBitmap, AnimationDirection animationDirection) {
        if (this.currentAnimationDirection == animationDirection) {
            this.setImageBitmap(newBitmap);
        } else if (animationDirection == AnimationDirection.LEFT) {
            this.setIncomingAnimation(bgImageInLeftAnimation);
            this.setOutgoingAnimation(bgImageOutLeftAnimation);
            this.setImageBitmap(newBitmap);
        } else if (animationDirection == AnimationDirection.RIGHT) {
            this.setIncomingAnimation(bgImageInRightAnimation);
            this.setOutgoingAnimation(bgImageOutRightAnimation);
            this.setImageBitmap(newBitmap);
        }
        this.currentAnimationDirection = animationDirection;
    }

    /**
     * updateCurrentBackground
     *
     * @param pager
     * @param direction
     */
    public void updateCurrentBackground(EcPager pager, final AnimationDirection direction) {
        int position = pager.getCurrentPage();
        Integer mainBgImageDrawableResource = dataSet.get(position).getMainBackgroundResource();
        setImageBitmapWithAnimation(mainBgImageDrawableResource, direction);
    }

    /**
     * updateCurrentBackgroundAsync
     *
     * @param pager
     * @param direction
     */
    public void updateCurrentBackgroundAsync(EcPager pager, final AnimationDirection direction) {
        int position = pager.getCurrentPage();
        Integer mainBgImageDrawableResource = dataSet.get(position).getMainBackgroundResource();
        if (mainBgImageDrawableResource == null) {
            return;
        }
        setImageBitmapWithAnimation(mainBgImageDrawableResource, direction);
    }

    /**
     * setDataSet
     *
     * @param dataSet
     */
    public void setDataSet(List<EcCardData> dataSet) {
        this.dataSet = dataSet;
    }

    /**
     * setImageBitmap
     *
     * @param bitmap
     */
    private void setImageBitmap(int bitmap) {
        showNext();
        Image image = (Image) this.getCurrentComponent();
        image.setPixelMap(bitmap);
    }

    /**
     * createBgImageInAnimation
     *
     * @param fromX
     * @param toX
     * @param transitionDuration
     * @param alphaDuration
     * @return animatorProperty
     */
    private AnimatorProperty createBgImageInAnimation(int fromX, int toX, int transitionDuration, int alphaDuration) {
        AnimatorProperty animatorProperty = new AnimatorProperty();
        animatorProperty.setCurveType(Animator.CurveType.LINEAR);
        animatorProperty.alphaFrom(0);
        animatorProperty.alpha(1);
        animatorProperty.moveFromX(fromX);
        animatorProperty.moveToX(toX);
        animatorProperty.setDuration(transitionDuration);
        return animatorProperty;
    }

    /**
     * createBgImageOutAnimation
     *
     * @param fromX
     * @param toX
     * @param transitionDuration
     * @return animatorProperty
     */
    private AnimatorProperty createBgImageOutAnimation(int fromX, int toX, int transitionDuration) {
        AnimatorProperty animatorProperty = new AnimatorProperty();
        animatorProperty.setCurveType(Animator.CurveType.LINEAR);
        animatorProperty.moveFromX(fromX);
        animatorProperty.moveToX(toX);
        animatorProperty.setDuration(transitionDuration);
        return animatorProperty;
    }

    /**
     * AnimationDirection
     *
     * @since 2021-05-31
     */
    enum AnimationDirection {
        LEFT, RIGHT
    }
}
